
var isLoading = false;
function bindevents(){
//var i = 0;//mark，为啥山寨xjq无效
//$(".border_wrap").each(function() {
//        $(this).delay(200*i).fadeIn(500);
//        i++;
//    });
    
$(".like_btn").click(function(){
		var $thispic = $(this);
		var like_num = $thispic.text();
		var new_like_num= Number(like_num)+1;
		$thispic.html(new_like_num);	
		$thispic.addClass ("liked_btn").removeClass("like_btn");
		$(this).unbind("click");
		$.post("face/add_like_num", {photoid : $thispic.attr('id')},
		function(data, textStatus) {
  			if (data.code === 1){
		
			}
			else if(data.code === -1){
				
				alert ("error!");
			}						
        }, "json");

	});

	$(".border_wrap").mouseover(function(){
		$(this).addClass ("facewall_pic").removeClass("border_wrap");
		$(this).find(".photo_time").css({visibility:"visible"});
		$(this).find(".like_btn").css({visibility:"visible"});
		$(this).find(".liked_btn").css({visibility:"visible"});
	});
	
	$(".border_wrap").mouseout(function (){
		$(this).addClass ("border_wrap").removeClass("facewall_pic");
		$(this).find(".photo_time").css({visibility:"hidden"});
		$(this).find(".like_btn").css({visibility:"hidden"});
		//$(this).find(".liked_btn").css({visibility:"hidden"});
	});
	
}

$('document').ready(function(){
	isLoading = false;
	bindevents();
	
	var winH = $(window).height();
	$(window).scroll(function () { 
		var pageH = $(document.body).height(); //页面总高度 
		var scrollT = $(window).scrollTop(); //滚动条top 
		var aa = (pageH-winH-scrollT)/winH; 
	    if ( aa<0.01 && !isLoading ){ 
	    	get_more_page();
    	}
	});
	
	$('#morebtn').click(function() {
		if( !isLoading ) {
			get_more_page();
		}
		return false;
	});
	
	
});


function get_more_page() {
	isLoading = true;
	$('#morebtn').html("正在载入中...");
	
	$.post("face/json_post_list", {
		
		last_id: parseInt($('#load_count').val())
	}, function(data, textStatus) {
	//alert ("!");
		if(textStatus=="timeout"){  
            $("#morebtn").html("加载超时，点此重试");
            isLoading = false;
        }
		else {
			if(data.code < 0) {
				$('#morebtn').html("已经没有更多真相了");
			}
			else if(data.code == 1) {
				
				//$('#load_count').val(1 + parseInt($('#load_count').val()));
				
				generate_more_page(data.results,data.end);
				if(data.results.length == 15){ //control:$config['per_page']
				$('#morebtn').html("点击此处加载更多...");
				isLoading = false;
				}
				else {
				$('#morebtn').html("已经没有更多真相了");
				isLoading = true;
				}
		    }
		}
	}, "json");
	return false;
}

function generate_more_page(photo_list,end) {
	$.get('assets/templates/facewall_single_page.html', function(data) {
	
			var c0=$('#facewall_column0').height();
			var c1=$('#facewall_column1').height();
			var c2=$('#facewall_column2').height();
			var c3=$('#facewall_column3').height();
		
		for(id in photo_list) {
			
			if((photo_list[id].post_title).length <= 12) {
				var posttitle = (photo_list[id].post_title);}
			else var posttitle = (photo_list[id].post_title).substr(0,12)+'...';
			
			if(photo_list[id].if_like == 1)	var likebtntype = "liked_btn";
			else var likebtntype = "like_btn";
			
			var view = {
				post_id: 		photo_list[id].post_id,
				photo_like: 	photo_list[id].photo_like,
				width: 			photo_list[id].width,
				height: 		photo_list[id].height,
				location: 		photo_list[id].location,
				photo_title: 	photo_list[id].photo_title,
				post_all_title: photo_list[id].post_title,
				post_title: 	posttitle,
				like_btn_type:	likebtntype,	
				photo_id:		photo_list[id].photo_id,
				ht:				parseInt(photo_list[id].height)*222/parseInt(photo_list[id].width)
			};
						
			var output = Mustache.render(data,view);
			
			/*alert($('#facewall_column0').height());
			alert(photo_list[id].photo_id);
			alert("loadcount:"+$('#load_count').val());
			}*/
			//alert(photo_list[id].height);
			switch (Math.min(c0,c1,c2,c3)){
			case c0:$('#facewall_column0').append(output);c0+=(60+view.ht);break;
			case c1:$('#facewall_column1').append(output);c1+=(60+view.ht);break;
			case c2:$('#facewall_column2').append(output);c2+=(60+view.ht);break;
			case c3:$('#facewall_column3').append(output);c3+=(60+view.ht);break;
			}

			$(".like_btn").unbind("click");
			bindevents();
			
		}
		//$('#load_count').val(photo_list[photo_list.length-1].photo_id);
		$('#load_count').val(end);
		//alert("loadcount:".$('#load_count').val());
        //isLoading = false;	
	});
	
}